export function photoCompress (file: Blob, imgAttr: any) {
  const img = new Image();
  img.src = URL.createObjectURL(file);

  return new Promise(resolve => {
    img.onload = function () {
      const that = this as any;
      let w = that.width;
      let h = that.height;
      const scale = w / h;
      w = imgAttr.width || w;
      h = imgAttr.height || w / scale;
      let quality = 0.7;
      const canvas = document.createElement("canvas");
      const ctx = canvas.getContext("2d");
      const anw = document.createAttribute("width");
      anw.nodeValue = w;
      canvas.setAttributeNode(anw);
      const anh = document.createAttribute("height");
      anh.nodeValue = h;
      canvas.setAttributeNode(anh);
      ctx.drawImage(that, 0, 0, w, h);
      if (imgAttr.quality && imgAttr.quality <= 1 && imgAttr.quality > 0) {
        quality = imgAttr.quality;
      }
      const imgType = imgAttr.type || "image/jpeg";
      canvas.toBlob(
        b => {
          resolve(b);
        },
        imgType,
        quality
      );
    };
  });
}
